package customer_relation_manage;

public class AnalysisOrder {
	/*
	 * 命令解析部分
	 */
	
	//得到控制台中除了命令之后的所有String信息
	public String getStringInf(String order){
		return order.substring(1);
		
	}
	
	//得到输入用户信息时所有Detail信息
	public String[] getCustomerInf(String order){
		String str= getStringInf(order);  //去首字母命令
		str=str.replaceAll(" ", "");      //去空格
		String[] strInf=str.split(",");
		return strInf;               //数组中存着输入的所有信息	
	}
	
	//根据输入的命令行ID信息得到要修改的Customer对象
	public int getCustomerById(int id) {
		int i = 0;
		for (; i < Init.clist.size(); i++) {
			if (Init.clist.get(i).getID()==id) {
				
			}
			else{
				i=-1;
			}				
		}
		return i;		
		
	}
	
	public void analysisOrder(String order){
		switch (order.charAt(0)) {
		case 'L'://查看功能
			String s=getStringInf(order);
			if (s.length()==0) {
				LookOver.check();
			}else {
//				System.out.println(s);
				LookOver.check(s);
//				LookOver.check("Bob");
			}
			break;
		case 'A':  //添加功能
			if (getStringInf(order).length()==0) {
				System.out.println("the inf added is empty,can not add");	
			}else {
				String [] strinf=getCustomerInf(order);
				for (int i = 0; i < strinf.length; i++) {
					strinf[i]=strinf[i].substring(strinf[i].indexOf(":")+1);
				}
				Customer c=new Customer(Integer.parseInt(strinf[0]),strinf[1] , 
						Integer.parseInt(strinf[2]), strinf[3], strinf[4], strinf[5]);
				AddCustomer.addCustomer(c);
				System.out.println("添加成功");
				LookOver.check();
			}
			
			break;
		case 'D'://删除功能
			if (getStringInf(order).length()==0) {
				System.out.println("Please insure have inputed ID you want to delete");
			}else {
				DeletCustomer.deleteCustomer(Integer.parseInt(getStringInf(order)));
				LookOver.check();
			}
			
			break;
		case 'U'://修改功能
			if (getStringInf(order).length()==0) {
				System.out.println("Please insure have inputed ID you want to delete");
			}else {
				String [] strinf=getCustomerInf(order);
				int id=Integer.parseInt(strinf[0].substring(strinf[0].indexOf(":")+1));
				int index=getCustomerById(id);
				System.out.println(strinf.length);
				for (int i = 0; i < strinf.length; i++) {
					switch (strinf[i].substring(0,strinf[i].indexOf(":"))) {//得到每个数组传递的是什么信息
					case "name":
						String sName=strinf[i].substring(strinf[i].indexOf(":")+1);
						System.out.println(sName);
						Init.clist.get(index).setName(sName);
						break;
					case "sex":
						String sSex=strinf[i].substring(strinf[i].indexOf(":")+1);
						int iSex=Integer.parseInt(sSex);
						Init.clist.get(index).setSex(iSex);
					    break;
					case "birthday":
						String sBirthday=strinf[i].substring(strinf[i].indexOf(":")+1);
						Init.clist.get(index).setBirthday(sBirthday);
						break;
					case "address":
						String sAddress=strinf[i].substring(strinf[i].indexOf(":")+1);
						Init.clist.get(id).setAddress(sAddress);
						break;
					case "call":
						String sCall=strinf[i].substring(strinf[i].indexOf(":")+1);
						Init.clist.get(id).setCall(sCall);
						break;

					default:
						break;
					}					
				}
				
			}	
			break;
		case 'O'://排序功能
			if (getStringInf(order)=="") {
				System.out.println("Please input order type you want");
			}else if(getStringInf(order).equals("ID")) {
				OrderCustomer.orderById();
//				LookOver.check();
			}else if(getStringInf(order).equals("NAME")){
				OrderCustomer.orderByName();
//				LookOver.check();
			}else {
				System.out.println("Please input correct order!!");
			}
			
			break;
		case 'H':     //Help功能
			System.out.println("********************查看帮助    性别0男1女********************");
			System.out.println("---------有参查看L NAME              无参查看L----------------");
			System.out.println("添加客户A id:xx,name：xx,sex:xx,birthday:xx,address:xx,call:xx");
			System.out.println("删除客户D xx(客户ID)                      ");
			System.out.println("修改客户信息U id:xx(必需),name：xx,sex:xx......               ");
			System.out.println("排序O ID/NAME（升序排列）                                                                             ");
			System.out.println("查看帮助H                                            退出程序Q");
			break;
		case 'Q':
			System.out.println("程序结束！！");
			System.exit(0);//退出程序
			break;
		default:
			break;
		}
		
	}

}
